<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form  :inline="true" label-width="80px" >
        <el-row>
          <el-col :span="10">
            <el-form-item label="机构名称">
              <el-select
                filterable
                clearable
                placeholder="请选择"
                v-model="params.hospital_id"
              >
                <el-option
                  v-for="item in hospitalNameList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="银行卡号:">
              <el-input v-model="params.bank_number" placeholder="请输入银行卡号" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="户名:">
              <el-input v-model ='params.bank_user_name' placeholder="请输入户名" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="审核时间">
              <el-date-picker
                v-model="data"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="审核状态">
              <el-select
                clearable
                placeholder="请选择"
                v-model="params.bank_status"
              >
                <el-option
                  v-for="item in statusList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="开户类型">
              <el-select
                clearable
                placeholder="请选择"
                v-model="params.bank_type"
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-button  type="primary" @click="search()">查询</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- table -->
    <div v-loading="tableLoading" class="table">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          fixed="left"
          align="center"
          :index="table_index"
          width="50"
        />
        <el-table-column prop="hospital_name" align="center" label="机构名称" width="180" />
        <el-table-column prop="bank_number" align="center" label="银行卡号" width="180" />
        <el-table-column prop="bank_name" align="center" label="开户银行" width="180"/>
        <el-table-column prop="bank_user_name" align="center" label="户名"  width="100" />
        <!-- 开户类型 -->
        <el-table-column prop="" label="开户类型" align="center"  width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.bank_type == 1"> 对公账户</span>
            <span v-if="scope.row.bank_type == 2"> 对私账户</span>
          </template>
        </el-table-column>
        <!-- 状态 -->
        <el-table-column prop="bank_status_cn_name" label="状态" align="center" width="180"/>
        <!-- 收款委托书 -->
        <el-table-column label="收款委托书" align="center" width="180" >
          <template slot-scope="scope">
            <div
              v-if="scope.row.letter_address"
              style="color: #409eef; cursor:pointer"
              @click="showImg(scope.row.letter_address)"
            >
              <img :src="scope.row.letter_address" alt style=" width: 100px; height: 100px;">
            </div>
          </template>
        </el-table-column>
        <!-- 申请说明 -->
        <el-table-column prop="apply_explain" label="申请说明" align="center" width="180"/>
        <!-- 审核说明 -->
        <el-table-column prop="return_explain" label="审核说明" align="center" width="180"/>
        <!-- 审核时间 -->
        <el-table-column prop="return_time" label="审核时间" align="center" width="180" />
        <!-- 申请时间 -->
        <el-table-column prop="created_at" label="申请时间" align="center" width="180" />

        <el-table-column  align="center" label="操作"  fixed = 'right'>
          <template slot-scope="scope">
          <el-button v-if="scope.row.bank_status_code == 0" type="text"  @click="editAdv(scope.row.id)">审核</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagesCon">
      <div>
        <el-pagination
          :current-page="params.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="params.pre_page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 审核 -->
    <el-dialog title="审核" :visible.sync="dialogVisible" width="468px">
      <el-form
        :inline="true"
        label-width="80px"
        label-position="right"
        :model="apply"
        :rules="rules"
        ref="form"
      >
        <el-form-item label="审核意见"   prop="bank_status">
          <el-radio-group v-model="apply.bank_status" >
            <p style="display: flex;">
              <el-radio label='1'>审核通过</el-radio>
              <el-radio label='-1'>审核驳回</el-radio>
            </p>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核说明" prop="return_explain">
          <el-input v-model="apply.return_explain" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"  @click="submit('form')">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="查看收款委托书" :visible.sync="dialogImgVisible" style="text-align: center">
      <img :src="imgUrl" alt style="width:400px;">
    </el-dialog>
  </div>
</template>
<script>

  import { cardApply, getOrgBankCardList } from '../../api/NewRetailSystem'
import { getOrgList } from '../../api/accountMange'
export default {
  data() {
    var validateReturn = (rule, value, callback) => {
      if (this.apply.bank_status == -1 &&value === '') {
        callback(new Error('审核驳回请填写审核说明'));
      } else {
        callback();
      }
    };
    return {
      imgUrl:'',
      data:[],
      params: {
        page: 1,
        pre_page: 10,//
        hospital_id: '',//	机构名称
       	bank_number: '',//	银行卡
        bank_user_name: '',//	银行开户名称
        return_time_start: '',//	审核时间开始
        return_time_end: '',//	审核时间结束
        bank_status: '',//	审核状态 '0' 待审核, '1' 已审核, '-1' 审核失败
        bank_type: '',//	开户类型 1对公 2对私
      },
      hospitalNameList:[],//机构下拉list
      statusList:[
        {
          id:0,
          name:'待审核'
        },
        {
          id:1,
          name:'审核通过'
        },
        {
          id:-1,
          name:'审核驳回'
        },
      ],
      typeList:[
        {
          id:1,
          name:'对公账户'
        },
        {
          id:2,
          name:'对私账户'
        },
      ],
      tableData: [],
      apply: {
        id:'',//	银行卡ID
        bank_status:'',//	状态
        return_explain:'',//审核说明
      },
      total: 0,
      tableLoading: false,
      dialogVisible: false,
      dialogImgVisible:false,
      rules:{
        bank_status:[
          {
            required: true,
            message: "请选择审核意见",
            trigger: "blur",
          },
        ],
        return_explain:[
          {
            validator: validateReturn,
            trigger: 'blur'
          },
          {
            max: 50,
            message: '长度 50 个字符内',
            trigger: 'blur'
          }
        ]
      },
    }
  },

  //新加路由保存参数
  beforeRouteLeave(to, from, next) {
    // const answer = window.confirm("当前页面搜索条件将会被保存");
    //更新 put
    let keyname = this.$route.name;
    let value = JSON.stringify(this.params);
    sessionStorage.setItem(keyname, value);
    next();
  },
  mounted() {
    if (sessionStorage.getItem(this.$route.name)) {
      this.params = JSON.parse(sessionStorage.getItem(this.$route.name));
    }
    this.getList(this.params)
    this.getHosName()
  },
  watch: {
    // 审核时间
    data(val) {
      this.params.return_time_start = val ? val[0] : "";
      this.params.return_time_end = val ? val[1] : "";
    },
    //清空弹窗内容
    dialogVisible(val){
      if(val === false){
       this.apply.return_explain = ''
       this.apply.bank_status = ''
      }
    }
  },
  methods: {
    search() {
      // this.params.page = 1
      // console.log("this.params",this.params)
      this.getList(this.params)
    },
    //获取列表
    async getList(opt) {
      this.tableLoading = true
      let res = await getOrgBankCardList(opt)
      console.log(res)
      if (res.status == 200) {
        this.tableLoading = false
        this.tableData = res.data.data.data
        this.total = res.data.data.meta.total
      } else {
        this.$message({
          message: res.data.msg,
          type: 'warning'
        })
      }
    },

    // 获取机构名称
    async getHosName() {
      let res = await getOrgList();
      if (res.data.code == 200) {
        let resData = res.data.data;
        this.hospitalNameList = resData;
        this.hospitalArr = resData;
      } else {
        this.errorStatus(res.data.msg);
      }
    },

    //序号
    table_index(index) {
      return (this.params.page - 1) * this.params.pre_page + index + 1
    },

    //图片
    showImg(img){
      this.dialogImgVisible = true
      console.log("img",img)
      this.imgUrl = img

    },

    //审核
    editAdv(item){
      this.dialogVisible = true
      this.apply.id = item
      console.log(this.apply)
    },

    //审核提交
    async submit(form){
      this.$refs[form].validate((valid) => {
        if (valid) {
          cardApply(this.apply).then((res) => {
            if (res.data.status == 0) {
              this.$message({
                showClose: true,
                message: "审核成功",
                type: "success",
              });
              this.dialogVisible = false
              // this.$router.push("/BankCardManagement");
              this.search()
            } else {
              this.$message({
                showClose: true,
                message: res.data.message,
                type: "error",
              });
            }
          });
        } else {
          this.$message({
            showClose: true,
            message: "有必选项没填,否则不能提交哦~~",
            type: "warning",
          });
        }
      });

      // let res = await cardApply(this.apply);
      // console.log("res",res)


    },

    //分页
    handleSizeChange(val) {
      this.params.page = 1
      this.params.pre_page = val
      this.getList()
    },

    handleCurrentChange(val) {
      this.params.page = val
      this.getList(this.params)
    }
  }
}
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
